package br.com.gkarian.mydesk.model;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.validation.constraints.AssertTrue;

//import org.hibernate.annotations.CascadeType;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import br.com.gkarian.mydesk.anotacoes.BlockLogging;
import br.com.gkarian.mydesk.model.financeiro.ContaBancaria;
import br.com.gkarian.mydesk.model.financeiro.TipoTransacao;
import br.com.gkarian.mydesk.model.financeiro.Transacao;

@Entity
public class Usuario {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	private Integer id;

	@NotEmpty(message = "nome.vazio")
	@Length(min = 3, max = 12, message = "nome.tamanho")
	@Column(name = "nome", nullable = false, length = 12)
	private String nome;

	@NotEmpty(message = "sobrenome.vazio")
	@Length(min = 3, max = 15, message = "sobrenome.tamanho")
	@Column(name = "sobrenome", nullable = false, length = 15)
	private String sobrenome;

	@NotEmpty(message = "email.vazio")
	@Email(message = "digite.email.valido")
	@Length(min = 8, max = 30, message = "email.tamanho")
	@Column(name = "email", nullable = false, length = 30)
	private String email;

	@NotEmpty(message = "login.vazio")
	@Length(min = 4, max = 12, message = "login.tamanho")
	@Column(name = "login", nullable = false, length = 12, unique = true)
	private String login;

	@BlockLogging
	@NotEmpty(message = "senha.vazio")
	@Length(min = 3, max = 12, message = "senha.tamanho")
	@Column(name = "passw", nullable = false, length = 12)
	private String passw;

	@BlockLogging
	@Transient
	private String passw2;

	@Column(name = "auth", nullable = true, length = 1)
	private Integer auth;

	@OneToMany(mappedBy="usuario", cascade={CascadeType.ALL})
	private List<ContaBancaria> listContasBancarias;

	@OneToMany(mappedBy = "usuario", cascade=CascadeType.ALL)
	private List<TipoTransacao> listTipoTransacao;

	//TODO: Esse relacionamento eh necessario msm?!
	@OneToMany(mappedBy = "usuario", cascade=CascadeType.ALL)
	private List<Transacao> listTransacao;

	// VALIDACOES

	@AssertTrue(message = "senhas.informadas.com.valores.diferentes")
	public boolean isVerificaSenha() {
		if (passw == null || passw.isEmpty() || !passw.equals(getPassw2())) {
			return false;
		}
		return true;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getSobrenome() {
		return sobrenome;
	}

	public void setSobrenome(String sobrenome) {
		this.sobrenome = sobrenome;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassw() {
		return passw;
	}

	public void setPassw(String passw) {
		this.passw = passw;
	}

	public String getPassw2() {
		return passw2;
	}

	public void setPassw2(String passw2) {
		this.passw2 = passw2;
	}

	public Integer getAuth() {
		return auth;
	}

	public void setAuth(Integer auth) {
		this.auth = auth;
	}

	public List<ContaBancaria> getListContasBancarias() {
		return listContasBancarias;
	}

	public void setListContasBancarias(List<ContaBancaria> listContasBancarias) {
		this.listContasBancarias = listContasBancarias;
	}

	public List<TipoTransacao> getListTipoTransacao() {
		return listTipoTransacao;
	}

	public void setListTipoTransacao(List<TipoTransacao> listTipoTransacao) {
		this.listTipoTransacao = listTipoTransacao;
	}

	public List<Transacao> getListTransacao() {
		return listTransacao;
	}

	public void setListTransacao(List<Transacao> listTransacao) {
		this.listTransacao = listTransacao;
	}

	@Override
	public String toString() {
		return "Usuario [id=" + id + ", nome=" + nome + ", sobrenome="
				+ sobrenome + ", email=" + email + ", login=" + login + "]";
	}


}